// pages/luck-draw/luck-draw.js
var o,page=1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navScrollLeft: 0,
    rq:'0',
    sj:'0',
    cate: [],
    dj:'1',
    dq:'',
    oneday:'afternoon',
    sign_log_list:[],
    user_info:{},
    windowWidth:0,
    cost_integral:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    getApp().page.onLoad(this, t)
    o = this;
    if (wx.getStorageSync('ACCESS_TOKEN')) {
      console.log('已登录')
      if (t.recommender_id) {
        console.log('1')
        getApp().page.bindParent1(t.recommender_id)
      } else {
        console.log('2')
      }
    } else {
      console.log('未登录')
      var recommender_id = wx.getStorageSync('recommender_id')
      console.log('recommender_id==', recommender_id)
      if (t.recommender_id) {
        if (recommender_id === '' || recommender_id === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', t.recommender_id)
        } else {
          console.log('you')
        }
      }
    }
    wx.getSystemInfo({
      success: (res) => {
          this.setData({
              pixelRatio: res.pixelRatio,
              windowHeight: res.windowHeight,
              windowWidth: res.windowWidth
          })
      },
  })
  },
  onShow:function(e){
    page = 1
    o.userindex()
    if(o.data.dj == 1){
      console.log('dq',o.data.dq)
      if(o.data.dq!=''){
        o.integral_lottery_index(o.data.dq,o.data.rq,o.data.sj)
      }else{
        o.integral_lottery_index('',o.data.rq,o.data.sj)
      }
    }else{
      o.setData({
        sign_log_list:[]
      })
      o.sign_log();
    }
   
  },
  userindex: function (e) {
    getApp().request({
      url: getApp().api.user.index,
      success: function (a) {
        if (0 == a.code) {
          o.setData({
            user_info: a.data.user_info
          })
        }
      }
    });
  },
  integral_lottery_index: function (c,t,e) {
    // var list = o.data.list
    getApp().request({
      url: getApp().api.integral_lottery.index_second,
      success: function (r) {
        console.log('人气好物', r)
        if (r.code === 0) {
          // if(c!=''){
          //   var dq = c
          // }else{
          //   var dq = r.data.list.title
          // }
          o.setData({
            list:r.data.list,
            cost_integral:r.data.cost_integral,
            // dq:dq,
            rq:t,
            sj:e
          })

        }
      },
      complete: function (r) {
        getApp().core.hideLoading();
      }
    });
  },
  register_index:function(e){
    wx.navigateTo({
      url: '/pages/integral-mall/register/index',
    })
  },
  sign_log:function(e){
    var sign_log_list = o.data.sign_log_list
    getApp().request({
      url: getApp().api.integral_lottery.sign_log,
      data:{
        page:page
      },
      success: function (r) {
        console.log(r)
        if (r.code === 0) {
          for (var i = 0; i <  r.data.list.length; i++) {
            sign_log_list.push(r.data.list[i])
          }
          o.setData({
            sign_log_list:sign_log_list
          })
        }
      },
      complete: function (r) {
        getApp().core.hideLoading();
      }
    });
  },
  oneday:function(e){
    var index = e.currentTarget.dataset.index;
    var singleNavWidth = this.data.windowWidth / 4;
    console.log('123',o.data.dq,index)

    o.setData({
      sj:index,
      navScrollLeft: (index - 1.55) * singleNavWidth
    })
  },
  dq:function(e){
    var dq = e.currentTarget.dataset.dq
    var rq = e.currentTarget.dataset.index
    console.log(dq,rq)
    o.setData({
      dq:dq,
      rq:rq,
      navScrollLeft:0,
      sj:'0'
    })
  },
  dj:function(e){
    var dd = e.currentTarget.dataset.dd
    if(dd == 1){
      o.setData({
        dj:1,
        sign_log_list:[]
      })
      page = 1
      o.integral_lottery_index('',0,0)
    }else{
      o.setData({
        dj:2,
        sign_log_list:[]
      })
      page = 1
      o.sign_log();

    }
  },
  luck_draw_details: function (e) {
    console.log(e)
    var item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '../luck-draw-details/luck-draw-details?item=' + encodeURIComponent(item),
    })
  },
  address_picker:function(e){
    var activity_id = e.currentTarget.dataset.activity_id
    wx.navigateTo({
      url: '/pages/address-picker/address-picker?cj=2' + '&activity_id=' + activity_id,
    })
  },
  luck_detail:function(e){
    console.log(e)
    var activity_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../luck-express-detail/luck-express-detail?activity_id=' + activity_id,
    })
  },
  integral_mall:function(e){
    wx.navigateTo({
      url: '/pages/tuibao-integral-mall/detail/index',
    })
  },
  onReachBottom: function (t) {
    var a = this
    getApp().page.onReachBottom(this);
    if(a.data.dj== 1 ){

    }else{
      page += 1
      a.sign_log();
    }

  },
    //转发
    onShareAppMessage: function () {
      var t = getApp().getUser()
      getApp().page.onShareAppMessage(this);
      return {
        path: "/pages/free/free?recommender_id=" + t.id,
        title: '爱心抽奖',
        imageUrl: 'https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F1a96543fc2031a4e0111a4aa7eae812b47160ddd.jpg'
      };
    },

})